package cn.iocoder.yudao.module.flgeo.service.geoshp;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.flgeo.controller.admin.geoshp.vo.*;
import cn.iocoder.yudao.module.flgeo.dal.dataobject.geoshp.GeoshpDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.flgeo.convert.geoshp.GeoshpConvert;
import cn.iocoder.yudao.module.flgeo.dal.mysql.geoshp.GeoshpMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.flgeo.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 图形 Service 实现类
 *
 * @author fl
 */
@Service
@Validated
public class GeoshpServiceImpl implements GeoshpService {

    @Resource
    private GeoshpMapper geoshpMapper;

    @Override
    public Long createGeoshp(GeoshpCreateReqVO createReqVO) {
        // 插入
        GeoshpDO geoshp = GeoshpConvert.INSTANCE.convert(createReqVO);
        String geom = geoshp.getGeom();
        geoshpMapper.insert(geoshp.setGeom(null));
        geoshpMapper.updateGeomById(geoshp.getId(),geom);
        // 返回
        return geoshp.getId();
    }

    @Override
    public void updateGeoshp(GeoshpUpdateReqVO updateReqVO) {
        // 校验存在
//        validateGeoshpExists(updateReqVO.getId());
        // 更新
        GeoshpDO updateObj = GeoshpConvert.INSTANCE.convert(updateReqVO);
        String geom = updateObj.getGeom();
        geoshpMapper.updateById(updateObj.setGeom(null));
        geoshpMapper.updateGeomById(updateObj.getId(),geom);
    }

    @Override
    public void deleteGeoshp(Long id) {
        // 校验存在
        validateGeoshpExists(id);
        // 删除
        geoshpMapper.deleteById(id);
    }

    private void validateGeoshpExists(Long id) {
        if (geoshpMapper.selectById(id) == null) {
            throw exception(GEOSHP_NOT_EXISTS);
        }
    }

    @Override
    public GeoshpDO getGeoshp(Long id) {
        return geoshpMapper.selectById(id);
    }

    @Override
    public List<GeoshpDO> getGeoshpList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return geoshpMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GeoshpDO> getGeoshpPage(GeoshpPageReqVO pageReqVO) {
        return geoshpMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GeoshpDO> getGeoshpList(GeoshpExportReqVO exportReqVO) {
        return geoshpMapper.selectList(exportReqVO);
    }

}
